<template>
  <div class="new-list">
    <!-- <div> -->
    <el-button
      type="primary"
      icon="el-icon-plus"
      class="add"
      circle
      @click="showDialog"
    ></el-button>
    <!-- </div> -->
    <el-table
      :data="tableData"
      style="width: 100%"
      height="570"
      @row-click="clickRow"
      stripe
      size="small"
    >
      <el-table-column prop="NewTitle" label="名称"> </el-table-column>
      <el-table-column prop="Status" label="状态"> </el-table-column>
      <el-table-column prop="CreateDate" label="时间"> </el-table-column>
    </el-table>
    <el-dialog
      :title="newTitle"
      :visible.sync="dialogFormVisible"
      width="500px"
      center
      :show-close="false"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
    >
      <el-form :model="form">
        <el-form-item label="名称" :label-width="formLabelWidth">
          <el-input
            v-model="form.newtitle"
            autocomplete="off"
            placeholder="请输入名称"
            @keyup.enter.native="add()"
          ></el-input>
        </el-form-item>
        <el-form-item label="内容" :label-width="formLabelWidth">
          <el-input
            type="textarea"
            :rows="6"
            v-model="form.content"
            autocomplete="off"
            placeholder="请输入内容"
            @keyup.enter.native="add()"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="add()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "NewList",
  data() {
    return {
      tableData: [],
      dialogFormVisible: false,
      form: {
        newtitle: "",
        content: "",
      },
      formLabelWidth: "60px",
    };
  },
  props: ["typeId", "newTitle"],
  mounted() {
    // console.log(this.typeId);
    // console.log(this.newTitle);
    this.getList();
  },
  methods: {
    clickRow(row, column, event) {
      // console.log(row);
      this.ToServiceDetail(row);
    },
    showDialog() {
      this.dialogFormVisible = true;
    },
    getList() {
      this.$axios({
        url: "/New/GetNewsList",
        method: "post",
        data: {
          userdata: this.$cookies.get("userdata"),
          typeid: this.typeId,
          limit: 100,
          page: 1,
        },
        transformRequest: [
          function (data) {
            let ret = "";
            for (let it in data) {
              ret +=
                encodeURIComponent(it) +
                "=" +
                encodeURIComponent(data[it]) +
                "&";
            }
            return ret;
          },
        ],
        headers: {
          "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
        },
      })
        .then((res) => {
          // console.log(res);
          this.tableData = res?.data?.data;
        })
        .catch((err) => {
          console.log(err);
        });
    },
    add() {
      if (!this.form.newtitle) {
        this.$message({
          message: "名称不能为空",
          type: "warning",
        });
        return false;
      }
      if (!this.form.content) {
        this.$message({
          message: "内容不能为空",
          type: "warning",
        });
        return false;
      }
      this.$axios({
        url: "/New/AddNewInfo",
        method: "post",
        data: {
          userdata: this.$cookies.get("userdata") || "",
          newtitle: this.form.newtitle,
          content: this.form.content,
          typeid: this.typeId,
        },
        transformRequest: [
          function (data) {
            let ret = "";
            for (let it in data) {
              ret +=
                encodeURIComponent(it) +
                "=" +
                encodeURIComponent(data[it]) +
                "&";
            }
            return ret;
          },
        ],
        headers: {
          "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
        },
      })
        .then((res) => {
          // console.log(res);
          if (res.data.status == 1) {
            this.$message({
              message: "保存成功",
              type: "success",
            });
            this.dialogFormVisible = false;
            this.getList();
            // this.$router.go(0);
          }
        })
        .catch((err) => {
          console.log(err);
        });
    },
    ToServiceDetail(newdata) {
      this.$router.push({ name: "ServiceDetail", params: { newdata } });
      //   this.$cookies.set("TopActive", 7);
    },
  },
};
</script>

<style scoped>
.add {
  position: absolute;
  z-index: 9;
  top: 20px;
  right: 40px;
}
</style>